<template>
	<div class="sys-file-container">
		<el-dialog v-model="state.isShowDialog" draggable overflow destroy-on-close width="500px">
			<template #header>
				<div style="color: #fff">
					<el-icon size="16" style="margin-right: 3px; display: inline; vertical-align: middle"> <ele-Edit /> </el-icon>
					<span> {{ props.title }} </span>
				</div>
			</template>
			<el-form :model="state.ruleForm" ref="ruleFormRef" label-width="auto">
				<el-row>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item :label="$t('message.file_label.fileName')" prop="fileName" :rules="[{ required: true, message: '文件名称不能为空', trigger: 'blur' }]">
							<el-input v-model="state.ruleForm.fileName" :placeholder="$t('message.file_label.fileName')" clearable />
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item :label="$t('message.file_label.fileType')" prop="fileType">
							<el-select v-model="state.ruleForm.fileType" :placeholder="$t('message.file_label.selFileType')" style="margin-bottom: 10px">
								<el-option :label="$t('message.file_type.relatedFile')" :value="$t('message.file_type.relatedFile')" />
								<el-option :label="$t('message.file_type.archiveFiles')" :value="$t('message.file_type.archiveFiles')" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item :label="$t('message.file_label.isPublic')">
							<el-radio-group v-model="state.ruleForm.isPublic">
								<el-radio :value="false">{{ $t('message.formI18nBtn.no') }}</el-radio>
								<el-radio :value="true">{{ $t('message.formI18nBtn.yes') }}</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="cancel">{{ $t('message.formI18nBtn.cancel') }}</el-button>
					<el-button type="primary" @click="submit">{{ $t('message.formI18nBtn.confirm') }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts" setup name="sysEditFile">
import { reactive, ref } from 'vue';
import { ElMessage } from 'element-plus';

import { getAPI } from '/@/utils/axios-utils';
import { SysFileApi } from '/@/api-services/api';
import { FileInput } from '/@/api-services/models';
import { useI18n } from 'vue-i18n';

const { t } = useI18n();
const props = defineProps({
	title: String,
	sysFileId: Number,
});
const emits = defineEmits(['handleQuery']);
const ruleFormRef = ref();
const state = reactive({
	isShowDialog: false,
	ruleForm: {} as FileInput,
});

// 打开弹窗
const openDialog = (row: any) => {
	state.ruleForm = JSON.parse(JSON.stringify(row));
	state.isShowDialog = true;
	ruleFormRef.value?.resetFields();
};

// 关闭弹窗
const closeDialog = () => {
	emits('handleQuery');
	state.isShowDialog = false;
};

// 取消
const cancel = () => {
	state.isShowDialog = false;
};

// 提交
const submit = () => {
	ruleFormRef.value.validate(async (valid: boolean) => {
		if (!valid) return;
		if (state.ruleForm.id != undefined && state.ruleForm.id > 0) {
			await getAPI(SysFileApi)
				.apiSysFileUpdatePost(state.ruleForm)
				.then((rsp: any) => {
					if (rsp.data.code == 200) {
						ElMessage.success(t('message.file_msg_info.updateFileSuccess') );
					} else {
						ElMessage.error(t('message.file_msg_info.updateFileError') + rsp.data.message);
					}
				});
		}
		closeDialog();
	});
};

// 导出对象
defineExpose({ openDialog });
</script>
